﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VehicleManagement.ControllersServices;
using VehicleManagement.Models;
using VehicleManagement.Codes;
using System.Data.Linq;

namespace VehicleManagement
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public bool Login(string driverLoginName, string password)
        {
            DriverServices ds = new DriverServices();
            return ds.DriverLogin(driverLoginName, password);
        }

        public Track GetTrack(string driverLoginName)
        {
            DriverServices ds = new DriverServices();
            int id = ds.GetDriverIdByLoginName(driverLoginName);
            Table<Track> tracks = VehicleManagementDataContextManager.Instance.GetTable<Track>();
            Track track = tracks.Single(t => t.DriverID == id && t.State == 0);
            return track;
        }

        public bool SubmitPoints(List<TrackPoint> points)
        {
            VehicleManagementDataContext context = VehicleManagementDataContextManager.Instance;
            Table<TrackPoint> trackPoints = context.GetTable<TrackPoint>();
            foreach (TrackPoint point in points)
            {
                point.TrackPointID = Guid.NewGuid();
            }
            try
            {
                trackPoints.InsertAllOnSubmit(points);
                context.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool UpdateTrack(int id, float timeCost, int state)
        {
            VehicleManagementDataContext context = VehicleManagementDataContextManager.Instance;
            Track track = context.GetTable<Track>().Single(t => t.TrackID == id);
            track.TimeCost = timeCost;
            track.State = state;
            try
            {
                context.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text = Login("123012009001", "12356").ToString();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {

            Track track = GetTrack("123012009001");
            Label1.Text = track.ApplyTime.ToString() + track.DriverName.ToString() + track.DriverName.ToString();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            TrackPoint trackPoint = new TrackPoint();
            List<TrackPoint> list = new List<TrackPoint>();
            trackPoint.TrackID = 6;
            trackPoint.Latitude = 123;
            trackPoint.Longitude = 34;
            list.Add(trackPoint);
            SubmitPoints(list);
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            UpdateTrack(6, 12, 0);
        }
    }
}